<?php
/**
 * Created by PhpStorm.
 * User: mac1
 * Date: 16/6/10
 * Time: 上午10:32
 */

namespace app\api\controller;
use think\Db;
require_once '../extend/tencent/TLSSigAPIv2.php';

class Meeting extends Base
{
    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 1103){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }

    }
    /**
     * 创建会议
     *
     * @return string
     */
    public function createMeet(){
        extract(input());
        $today = date('Y-m-d H:i:s');
        $userInfo = $this->getUserInfo($user_id,'user_type,user_auth,user_hospital',2);
        if($userInfo['user_type'] == 1){
            return json(['status'=>0,'msg'=>'您不能发起会议']);
        }
        if($m_start >= $m_end){
            return json(['status'=>0,'msg'=>'开始时间和结束时间有问题']);
        }
        if($m_start <= $today){
            return json(['status'=>0,'msg'=>'开始时间不能小于当前时间']);
        }
        //-----------------------
        $canNew = $this->canVisitMeet($user_id,$m_start,$m_end);
        if($canNew == false){
            return json(['status'=>100,'msg'=>'此时间段有未完成的拜访或会议']);
        }
        //-----------------------
        $meets = Db::table('up_meeting')->count();
        $week_array = ["周日","周一","周二","周三","周四","周五","周六"];
        $data = array();
        $data['m_id'] = $this->uuid();
        $data['m_group_id'] = $meets + $user_id + rand(100000,999999);
        $data['m_number'] = time() . rand(100000,999999);
        $data['m_name'] = $m_name;
        $data['m_type'] = $m_type;
        $data['m_user_id'] = $user_id;
        $data['m_c_id'] = $userInfo['user_hospital'];
        $data['m_status'] = 1;
        $data['m_start'] = $m_start;
        $data['m_end'] = $m_end;
        $data['m_day'] = substr($m_start,0,10);
        $data['m_week'] = $week_array[date('w',strtotime($m_start))];
        $data['m_month'] = substr($m_start,0,7);
        $data['m_time'] = $today;
        $data['m_update'] = $today;
        $data['m_desc'] = $m_desc;
        $data['m_img'] = $m_img;
        $data['m_from'] = $userInfo['user_auth'];
        if($data['m_start'] <= $today){
            $data['m_status'] = 2;
        }
        if($data['m_type'] != 2){
            $data['m_address'] = $m_address;
            $data['m_cp_id'] = $cp_id; //Sae里有获取产品的方法
        }
        if($data['m_type'] != 3){
            $data['m_files'] = $m_files;
        }
        if($data['m_type'] == 2){
            $data['m_nums'] = $m_nums;
            $data['m_upload'] = $m_upload;
        }
        //--------------------------------------------------------------------------------------------------------
        $api = new \Tencent\TLSSigAPIv2(config('tengx')['appid'],config('tengx')['appkey']);
        $usersig = $api->genUserSig( "administrator" );
        $name = '孚玛会议' . time();
        $res = $this->create($user_id,$data['m_group_id'],$name,[],$usersig);
        if ($res['ActionStatus'] == 'OK' && $res['ErrorCode'] == 0) {
            Db::startTrans();
            try {
                $meetid = $data['m_id'];
                Db::table('up_meeting')->insert($data);
                //-----------------
                $meet_u = [
                    'mu_m_id' => $meetid,
                    'mu_user_id' => $user_id,
                    'mu_status' => 2,
                    'mu_auth' => 3,
                    'mu_time' => $today,
                    'mu_verify_time' => $today,
                    'mu_update' => $today,
                ];
                Db::table('up_meeting_user')->insert($meet_u);
                Db::commit();
                $hour = round((strtotime($m_start)-time())/3600,1);
                if($m_type != 2){
                    $cp_name = Db::table('up_company_product')->where('cp_id',$cp_id)->value('cp_name');
                    $content = '您发布了'.$cp_name.'产品的'.$m_name.'会议，会议在'.$hour.'小时之后开始，请知悉：';
                }else{
                    $content = '您发布了'.$m_name.'会议，会议在'.$hour.'小时之后开始，请知悉：';
                }
                //
                allInNotice('会议发布提醒',$content,2,$user_id,$meetid);
                try {
                    $this->Jpush(1, $user_id, '您发布了新的会议', ['id' => $meetid, 'n_type' => 2]);
                } catch (\Exception $e) {
                }
                return json(['status'=>200,'msg'=>$meetid]);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['status'=>0,'msg'=>'新建失败，请刷新测试']);
            }
        }else{
            return json(['status'=>0,'msg'=>'初始化失败，请刷新测试']);
        }
        //--------------------------------------------------------------------------------------------------------

    }

    /**
     * 编辑会议
     *
     * @return string
     */
    public function editMeet(){
        extract(input());
        $user_type = $this->getUserInfo($user_id,'user_type');
        if($user_type == 1){
            return json(['status'=>0,'msg'=>'您不能编辑会议']);
        }
        $meet = Db::table('up_meeting')->where('m_id',$m_id)->find();
        $today = date('Y-m-d H:i:s');
        $data = array();
        $data['m_name'] = $m_name;
        $data['m_update'] = $today;
        $data['m_desc'] = $m_desc;
        $data['m_img'] = $m_img;
        if($meet['m_type'] != 2){
            $data['m_address'] = $m_address;
        }
        if($meet['m_type'] != 3){
            $data['m_files'] = $m_files;
        }
        if($meet['m_type'] == 2){
            $data['m_upload'] = $m_upload;
        }
        if($meet['m_status'] == 1){ //未开始时可以传
            $week_array = ["周日","周一","周二","周三","周四","周五","周六"];
            $data['m_start'] = $m_start;
            $data['m_end'] = $m_end;
            $data['m_day'] = substr($m_start,0,10);
            $data['m_week'] = $week_array[date('w',strtotime($m_start))];
            $data['m_month'] = substr($m_start,0,7);
        }
        $res = Db::table('up_meeting')->where('m_id',$m_id)->update($data);
        if($res){
            $this->editLikes(3,$m_id);
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>100,'msg'=>'操作失败，请刷新重试']);
        }

    }

    /**
     * 我的会议（医生专用）
     * type：1我预约的会议  2我的参会记录
     * @return string
     */
    public function myMeetOne(){
        extract(input());
        $this->meetStatus();
        $where = [
            'mu_user_id' => $user_id,
            'm_del' => 1,
        ];
        if($type == 2){
            $where['mu_status'] = 2;
        }
        $res = Db::table('up_meeting_user')->alias('mu')
            ->join('up_meeting m','mu.mu_m_id = m.m_id','left')
            ->join('up_u_user u','m.m_user_id = u.user_id','left')
            ->where($where)
            ->order('m_status asc,mu_join asc,m_start desc')
            ->page($page,10)
            ->field('m.*,mu_id,mu_status,mu_join,user_id,user_username,user_image,user_auth')
            ->select();
        return json(['status'=>200,'msg'=>$res]);
    }
    /**
     * 我的会议（业务员专用）
     * type：1我发起的  2我参与的
     * m_type：1普通会议  2孚玛会议
     * @return string
     */
    public function myMeetTwo(){
        extract(input());
        $this->meetStatus();
        $where = [
            'mu_user_id' => $user_id,
            'm_del' => 1,
        ];
        if($type == 1){
            $where['mu_auth'] = 3;
        }else{
            $where['mu_auth'] = ['<>',3];
        }
        if($m_type == 1){
            $where['m_type'] = ['<>',2];
        }else{
            $where['m_type'] = 2;
        }
        $res = Db::table('up_meeting_user')->alias('mu')
            ->join('up_meeting m','mu.mu_m_id = m.m_id','left')
            ->join('up_u_user u','m.m_user_id = u.user_id','left')
            ->where($where)
            ->order('m_status asc,mu_join asc,m_start desc')
            ->page($page,10)
            ->field('m.*,mu_id,mu_status,mu_join,user_id,user_username,user_image,user_auth')
            ->select();
        return json(['status'=>200,'msg'=>$res]);
    }
    /**
     * 我的某个会议详情
     * 返回值如果不是200，则什么都看不了，只显示msg即可
     * @return string
     */
    public function myMeetInfo(){
        extract(input());
        $today = date('Y-m-d H:i:s');
        $meet = Db::table('up_meeting m')
            ->join('up_u_user u','m.m_user_id = u.user_id','left')
            ->where('m_id',$m_id)
            ->field('m.*,user_id,user_username,user_image,user_auth')
            ->find();
        $have = Db::table('up_meeting_user')
            ->where('mu_m_id',$m_id)
            ->where('mu_user_id',$user_id)
            ->find();
        if($meet['m_type'] == 2){
            $api = new \Tencent\TLSSigAPIv2(config('tengx')['appid'],config('tengx')['appkey']);
            $usersig = $api->genUserSig( "administrator" );
            if(!$meet['m_group_id']){
                $meets = Db::table('up_meeting')->count();
                $new_gid = $meets + $user_id + rand(100000,999999);
                $name = '孚玛会议' . time();
                $res = $this->create($meet['m_user_id'],$new_gid,$name,[],$usersig);
                if ($res['ActionStatus'] == 'OK' && $res['ErrorCode'] == 0) {
                    Db::table('up_meeting')->where('m_id',$m_id)->setField('m_group_id',$new_gid);
                }elseif ($res['ErrorCode'] == 10021) {
                    Db::table('up_meeting')->where('m_id',$m_id)->setField('m_group_id',$new_gid);
                }else{
                    return json(['status'=>100,'msg'=>'初始化数据失败，请刷新重试']);
                }
            }
            //孚玛会议不需要邀请，进来就能参与
            $have_num = Db::table('up_meeting_user')
                ->where('mu_m_id',$m_id)
                ->count();
            if(!$have && $meet['m_nums'] != 0 && $have_num >= $meet['m_nums']){
                return json(['status'=>100,'msg'=>'人数已满']);
            }
            if(!$have && $meet['m_status'] < 3){
                if($meet['m_nums'] == 0 || $have_num < $meet['m_nums']){
                    if($meet['m_group_id']){
                        $users = Db::table('up_u_user')->where('user_id',$user_id)
                            ->field('CAST(user_id AS CHAR) Member_Account')
                            ->select();
                        $res = $this->joinin($usersig,$meet['m_group_id'],$users);
                        if ($res['ActionStatus'] == 'OK' && $res['ErrorCode'] == 0) {
                            $ins = [
                                'mu_m_id' => $m_id,
                                'mu_user_id' => $user_id,
                                'mu_status' => 2,
                                'mu_time' => $today,
                                'mu_verify_time' => $today,
                                'mu_update' => $today
                            ];
                            $mu_id = Db::table('up_meeting_user')->insertGetId($ins);
                            //更新下have
                            $have = Db::table('up_meeting_user')
                                ->where('mu_id',$mu_id)
                                ->find();
                        }else{
                            return json(['status'=>100,'msg'=>'初始化失败，请刷新重试']);
                        }
                    }
                }
            }
        }
        if($have){
            $res = array_merge($meet,$have);
        }else{
            $res = $meet;
            $res['mu_status'] = 0; //0代表没有预约参加，需要预约
        }
        if($res['m_img']){
            $res['m_img'] = explode(',',$res['m_img']);
        }
        if($res['m_files']){
            $res['m_files'] = explode(',',$res['m_files']);
            //把之前的文件数组循环，获取图片，塞入数组，再替换之前的数组内容
            $images = [];
            foreach ($res['m_files'] as $k => $v){
                $fileimgs = Db::table('up_meeting_fileimg')
                    ->where('md_m_id',$m_id)
                    ->where('md_file_url',$v)
                    ->value('md_imgs');
                if($fileimgs){
                    $images[] = explode(',',$fileimgs);
                }else{
                    $pdfToImg = new \app\api\controller\Pdftoimg;
                    $imgs = $pdfToImg->toImgUploadQn($v);
                    Db::table('up_meeting_fileimg')->insert([
                        'md_m_id' => $m_id,
                        'md_file_url' => $v,
                        'md_imgs' => $imgs,
                    ]);
                    $images[] = explode(',',$imgs);
                }
            }
            $res['m_files'] = $images;
        }
        //非孚玛会议的产品名字
        if($res['m_type'] != 2){
            $cp = Db::table('up_company_product')->where('cp_id',$res['m_cp_id'])
                ->field('cp_name,cp_thumb')->find();
            $res['cp_name'] = $cp['cp_name'];
            $res['cp_thumb'] = $cp['cp_thumb'];
        }
        //收藏
        $like = Db::table('up_like_project')
            ->where('lpro_user_id', $user_id)
            ->where('lpro_type', 3)
            ->where('lpro_prom_id', $m_id)
            ->find();
        $res['like'] = 0;
        if($like){
            $res['like'] = 1;
        }
        $can = 1; //不可以
        if($res['m_upload'] == 1 || $res['mu_auth'] > 1){
            $can = 2;
        }
        return json(['status'=>200,'msg'=>$res,'can_upload'=>$can]);
    }
    public function upladMeetFile(){
        extract(input());
        $meet = Db::table('up_meeting')
            ->where('m_id',$m_id)
            ->field('m_files')
            ->find();
        $newfile = trim($newfile,',');
        $file = $newfile;
        if($meet['m_files']){
            $file = $meet['m_files'] . ',' . $newfile;
        }
        $update = [
            'm_files' => $file,
            'm_update' => date('Y-m-d H:i:s')
        ];
        $res = Db::table('up_meeting')
            ->where('m_id',$m_id)
            ->update($update);
        if($res){
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>100,'msg'=>'操作失败，请刷新重试']);
        }
    }
    /**
     * 参会
     * @return string
     */
    public function joinMeet(){
        extract(input());
        $today = date('Y-m-d H:i:s');
        $have = Db::table('up_meeting_user')
            ->where('mu_m_id',$m_id)
            ->where('mu_user_id',$user_id)
            ->find();
        if(!$have){
            return json(['status'=>0,'msg'=>'您无权参会']);
        }
        $status = Db::table('up_meeting')
            ->where('m_id', $m_id)
            ->value('m_status');
        if($status != 2){
            return json(['status'=>0,'msg'=>'会议不在进行中，不能参会']);
        }

        if($have['mu_join'] == 1){
            $res = Db::table('up_meeting_user')
                ->where('mu_m_id',$m_id)
                ->where('mu_user_id',$user_id)
                ->update([
                    'mu_join' => 2,
                    'mu_update' => $today
                ]);
            if($res){
                return json(['status'=>200,'msg'=>'操作成功']);
            }else{
                return json(['status'=>100,'msg'=>'操作失败，请刷新重试']);
            }
        }else{
            return json(['status'=>100,'msg'=>'您已经参会了']);
        }
    }
    /**
     * 申请参加会议（普通和线上）
     * @return string
     */
    public function askMeet(){
        extract(input());
        $today = date('Y-m-d H:i:s');
        $have = Db::table('up_meeting_user')
            ->where('mu_m_id',$m_id)
            ->where('mu_user_id',$user_id)
            ->find();
        $status = Db::table('up_meeting')
            ->where('m_id', $m_id)
            ->value('m_status');
        if($status > 2){
            return json(['status'=>0,'msg'=>'会议已经结束了，不能再申请']);
        }
        if($have){
            if($have['mu_status'] == 1){
                return json(['status'=>0,'msg'=>'您已经申请过了']);
            }
            if($have['mu_status'] == 2){
                return json(['status'=>0,'msg'=>'您已经通过了']);
            }
            $ins = [
                'mu_status' => 1,
                'mu_update' => $today
            ];
            $res = Db::table('up_meeting_user')->where('mu_id',$have['mu_id'])->update($ins);
        }else{
            $ins = [
                'mu_m_id' => $m_id,
                'mu_user_id' => $user_id,
                'mu_status' => 1,
                'mu_time' => $today,
                'mu_update' => $today
            ];
            $res = Db::table('up_meeting_user')->insert($ins);
        }
        if($res){
            //
            allInNotice('预约提醒','您预约的会议正在审核中，请知悉：',2,$user_id,$m_id);
            try {
                $this->Jpush(1, $user_id, '您预约的会议正在审核中', ['id' => $m_id, 'n_type' => 2]);
            } catch (\Exception $e) {
            }
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>100,'msg'=>'操作失败，请刷新重试']);
        }

    }
    /**
     * 结束
     * @return string
     */
    public function cancelMeet(){
        extract(input());
//        $own = Db::table('up_meeting')->where('m_id', $m_id)->value('m_user_id');
//        if($own != $user_id){
//            return json(['status'=>100,'msg'=>'只有发起人才可以操作！']);
//        }
        $res = Db::table('up_meeting')
            ->where('m_id', $m_id)
            ->setField('m_status',3);
        if($res){
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>100,'msg'=>'操作失败，请刷新重试']);
        }

    }
    /**
     * 查询
     * @return string
     */
    public function seeMeetAndMe(){
        $m_id = input('m_id');
        $user_id = input('user_id');
        $myAuth = Db::table('up_meeting_user')
            ->where('mu_m_id',$m_id)
            ->where('mu_user_id',$user_id)
            ->field('mu_auth,mu_line')
            ->find();
        $meetStatus = Db::table('up_meeting')
            ->where('m_id',$m_id)
            ->value('m_status');
        return json(['status'=>200,'msg'=>$myAuth['mu_auth'],'msgline'=>$myAuth['mu_line'],'meetmsg'=>$meetStatus]);

    }
    //----------------------------------------------------------------------

    /**
     * 查看参会人员
     */
    public function seeMeetUsers(){
        extract(input());
        Db::table('up_meeting_user')
            ->where('mu_m_id',$m_id)
            ->where('mu_auth','>',1)
            ->setField('mu_line',2);
        $own = Db::table('up_meeting')->where('m_id', $m_id)->value('m_user_id');
//        if($own != $user_id){
//            return json(['status'=>100,'msg'=>'只有发起人才可以操作！']);
//        }
        $where = [
            'mu_m_id' => $m_id,
//            'mu_user_id' => ['<>',$user_id]
        ];
        if(isset($type) && $type && $type > 0){
            $where['mu_status'] = $type;
        }
        $data = Db::table('up_meeting_user mu')
            ->join('up_u_user u','mu.mu_user_id = u.user_id','left')
            ->where($where)
            ->order('mu_status asc,mu_time asc')
            ->page($page,20)
            ->field('mu_id,mu_auth,mu_status,mu_remark,mu_time,mu_verify_time,user_id,user_username,user_image')
            ->select();
        if($data){
            foreach ($data as $k => $v){
                //提问的人是是否是好友
                $like = Db::table('up_friend')
                    ->where('friend_user_id', $user_id)
                    ->where('friend_other_user', $v['user_id'])
                    ->find();
                $data[$k]['friend'] = 0; //不是好友
                if($like){
                    $data[$k]['friend'] = 1; //是好友
                }
            }
        }
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 参会人员的同意和拒绝
     * type : 2同意  3拒绝
     */
    public function yesNoMeetUser(){
        extract(input());
        $meet_data = Db::table('up_meeting_user')->where('mu_id', $mu_id)
            ->field('mu_m_id,mu_user_id')->find();
        $m_id = $meet_data['mu_m_id'];
        $own = Db::table('up_meeting')->where('m_id', $m_id)->value('m_user_id');
        if($own != $user_id){
            return json(['status'=>100,'msg'=>'只有发起人才可以操作！']);
        }
        $today = date('Y-m-d H:i:s');
        $data = [
            'mu_verify_time' => $today,
            'mu_update' => $today,
            'mu_status' => $type,
        ];
        if($type == 2){
            $content = '您预约的会议已经审核通过，请知悉：';
            $data['mu_remark'] = '';
        }else{
            $content = '您预约的会议已被拒绝，拒绝原因：' . $remark . '，请知悉：';
            $data['mu_remark'] = $remark;
        }
        $res = Db::table('up_meeting_user')
            ->where('mu_id', $mu_id)
            ->update($data);
        if($res){
            //
            allInNotice('预约提醒',$content,2,$meet_data['mu_user_id'],$m_id);
            try {
                $this->Jpush(1, $meet_data['mu_user_id'], $content, ['id' => $m_id, 'n_type' => 2]);
            } catch (\Exception $e) {
            }
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>0,'msg'=>'操作失败，请刷新重试']);
        }
    }
    /**
     * 会议评价
     */
    public function meetComment(){
        extract(input());
        $have = Db::table('up_meeting_user')
            ->where('mu_m_id',$m_id)
            ->where('mu_user_id',$user_id)
            ->find();
        if(!$have){
            return json(['status'=>100,'msg'=>'您无权评论！']);
        }
        if($have['mu_comm_status'] != 1){
            return json(['status'=>100,'msg'=>'您已经评论过了！']);
        }
        $m_status = Db::table('up_meeting')
            ->where('m_id',$m_id)
            ->value('m_status');
        if($m_status != 3){
            return json(['status'=>100,'msg'=>'只有已结束的会议才可以评价！']);
        }
        $today = date('Y-m-d H:i:s');
        $data = [
            'mu_comm_status' => 2,
            'mu_comm_star' => $star,
            'mu_comm_content' => $content,
            'mu_comm_time' => $today,
        ];
        $res = Db::table('up_meeting_user')
            ->where('mu_id', $have['mu_id'])
            ->update($data);
        if($res){
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>0,'msg'=>'操作失败，请刷新重试']);
        }
    }
    //----------------------------------------------------------------------------------------------------------
    /**
     * 会议提问——显示（实时显示的话需要沟通下）
     * 非负责人看的话who大于0说明是回复
     */
    public function meetQuestion(){
        extract(input());
        $have = Db::table('up_meeting_user')
            ->where('mu_m_id',$m_id)
            ->where('mu_user_id',$user_id)
            ->find();
        if(!$have){
            return json(['status'=>100,'msg'=>'您没有参加此次会议！']);
        }else{
            if($have['mu_status'] != 2){
                return json(['status'=>100,'msg'=>'您未经过同意，不能参加会议！']);
            }
        }
//        $meet = Db::table('up_meeting')
//            ->where('m_id',$m_id)
//            ->find();
        if($have['mu_auth'] == 3){ //发起者（只有发起者才可以回复？）
            $where = [
                'mq_m_id' => $m_id,
                'mq_who' => 0
            ];
            if(isset($mq_id) && $mq_id && $mq_id > 0){
                $uids = Db::table('up_meeting_question')->alias('mq')
                    ->join('up_u_user u','mq.mq_user_id = u.user_id','left')
                    ->where($where)
                    ->where('mq_id','<=',$mq_id)
                    ->group('mq_user_id')
                    ->column('mq_user_id');
                if($uids){
                    $where['mq_user_id'] = ['not in' , $uids];
                }
                $where['mq_id'] = ['>' , $mq_id];
            }
            $data = Db::table('up_meeting_question')->alias('mq')
                ->join('up_u_user u','mq.mq_user_id = u.user_id','left')
                ->where($where)
                ->group('mq_user_id')
                ->field('mq.*,user_username,user_image')
                ->select();
        }else{
            //who大于0说明是回复
            $where = [
                'mq_m_id' => $m_id,
                'mq_user_id|mq_who' => $user_id
            ];
            if(isset($mq_id) && $mq_id && $mq_id > 0){
                $where['mq_id'] = ['>' , $mq_id];
            }
            $data = Db::table('up_meeting_question')->alias('mq')
                ->join('up_u_user u','mq.mq_user_id = u.user_id','left')
                ->where($where)
                ->order('mq_time asc')
                ->field('mq.*,user_type,user_auth,user_username,user_image')
                ->select();
        }
        return json(['status'=>200,'msg'=>$data,'meetAuth'=>$have['mu_auth']]);
    }
    //针对发起人回复页，单独显示某个用户的提问内容
    public function meetQuestionOne(){
        extract(input());
        //who大于0说明是回复
        $where = [
            'mq_m_id' => $m_id,
            'mq_user_id|mq_who' => $mq_user_id
        ];
        if(isset($mq_id) && $mq_id && $mq_id > 0){
            $where['mq_id'] = ['>' , $mq_id];
        }
        $data = Db::table('up_meeting_question')->alias('mq')
            ->join('up_u_user u','mq.mq_user_id = u.user_id','left')
            ->where($where)
            ->order('mq_time asc')
            ->field('mq.*,user_type,user_auth,user_username,user_image')
            ->select();
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 会议提问——提问或回复
     */
    public function meetQuestionReply(){
        extract(input());
        $today = date('Y-m-d H:i:s');
        $data = [
            'mq_m_id' => $m_id,
            'mq_user_id' => $user_id,
            'mq_content' => $content,
            'mq_time' => $today,
        ];
        if(isset($who) && $who){
            $data['mq_who'] = $who;
        }
        $res = Db::table('up_meeting_question')->insert($data);
        if($res){
            return json(['status'=>200,'msg'=>'发表成功！']);
        }else{
            return json(['status'=>100,'msg'=>'发表失败，请重试！']);
        }
    }
    /**
     * 查看讨论
     * @return mixed|\think\response\Json
     */
    public function meetTalks(){
        extract(input());
        $today = date('Y-m-d H:i:s');
        $where = [
            'mt_m_id' => $m_id
        ];
        if(isset($mt_id) && $mt_id && $mt_id > 0){
            $where['mt_id'] = ['>' , $mt_id];
        }
        $field = 'mt.*,user_username,user_image,user_type,user_auth';
        $reply = Db::table('up_meeting_talk')->alias('mt')
            ->join('up_u_user u','mt.mt_user_id = u.user_id','left')
            ->where($where)
            ->order('mt_time asc')
            ->field($field)
//            ->page($page,20)
            ->select();
        //-----
        $have = Db::table('up_meeting_user')
            ->where('mu_m_id',$m_id)
            ->where('mu_auth',3)
            ->find();
        return json(['status'=>200 , 'msg'=>$reply, 'authUid'=>$have['mu_user_id']]);
    }
    /**
     * 会议讨论-发布
     */
    public function meetTalksAdd(){
        extract(input());
        $today = date('Y-m-d H:i:s');
        $data = [
            'mt_m_id' => $m_id,
            'mt_user_id' => $user_id,
            'mt_content' => $content,
            'mt_time' => $today,
        ];
        $res = Db::table('up_meeting_talk')->insert($data);
        if($res){
            return json(['status'=>200,'msg'=>'发表成功！']);
        }else{
            return json(['status'=>100,'msg'=>'发表失败，请重试！']);
        }
    }
    /**
     * 会议二维码
     */
    public function meetErcode(){
        extract(input());
        $where = [
            'm_id' => $m_id
        ];
        $data = Db::table('up_meeting')->where($where)->find();
        if($data['m_ercode']){
            return json(['status'=>200,'msg'=>$data['m_ercode']]);
        }else{
            $ercode = Qrcode("$m_id");
            $res = Db::table('up_meeting')->where($where)->setField('m_ercode',$ercode);
            if($res){
                return json(['status'=>200,'msg'=>$ercode]);
            }else{
                return json(['status'=>100,'msg'=>'生成失败，请重试！']);
            }

        }
    }
    //------划线、主持人
    /**
     * 划线的人
     */
    public function meetLine(){
        extract(input());
        Db::table('up_meeting_user')
            ->where('mu_m_id',$m_id)
            ->where('mu_auth','>',1)
            ->setField('mu_line',2);
        $where = [
            'mu_m_id' => $m_id
        ];
        $users = Db::table('up_meeting_user')->alias('mu')
            ->join('up_u_user u','mu.mu_user_id = u.user_id','left')
            ->where($where)
            ->field('mu.*,user_username,user_id,user_image,user_phone')
//            ->page($page,20)
            ->select();
        return json(['status'=>200,'msg'=>$users]);
    }
    /**
     * 设置划线或取消划线
     */
    public function meetLineSet(){
        extract(input());
        $mu = Db::table('up_meeting_user')
            ->where('mu_id',$mu_id)
            ->field('mu_line')
            ->find();
        $line = 1;
        if($mu['mu_line'] == 1){
            $line = 2;
        }
        $res = Db::table('up_meeting_user')
            ->where('mu_id',$mu_id)
            ->setField('mu_line',$line);
        if($res){
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>100,'msg'=>'操作失败，请重试！']);
        }
    }
    /**
     * 设置主持人或取消主持人
     */
    public function meetAuthSet(){
        extract(input());
        $mu = Db::table('up_meeting_user')
            ->where('mu_id',$mu_id)
            ->field('mu_auth,mu_m_id')
            ->find();
        $res = Db::table('up_meeting_user')
            ->where('mu_id',$mu_id)
            ->setField('mu_auth',2);
        if($res){
            Db::table('up_meeting_user')
                ->where('mu_m_id',$mu['mu_m_id'])
                ->where('mu_id','<>',$mu_id)
                ->where('mu_auth','>',1)
                ->setField('mu_auth',1);
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>100,'msg'=>'操作失败，请重试！']);
        }
    }
}